home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / moveIKtoFK.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  7.4 KB  |  263 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  Dec, 2002
  22. //
  23. //<doc>
  24. //<name moveIKtoFK>
  25. //<owner "Alias|Wavefront Unsupported">
  26. //
  27. //<synopsis>
  28. //        moveIKtoFK
  29. //
  30. //<returns>
  31. //        None
  32. //
  33. //<description>
  34. //      Script for moving the selected ik handle or object connected to
  35. //      and ik handle to the location of the associated fk joint.
  36. //
  37. //<examples>
  38. //      Say you have an ikHandle in FK mode (i.e. ikBlend is set to zero),
  39. //      and you have performed a "Connect to FK/IK" operation to associate
  40. //      a driver object with the ikHandle.
  41. //
  42. //      You have rotated the joints in the handle so that now the driver
  43. //      and the ikHandle are no longer near to the joint.
  44. //        Select the ikHandle or the driver object and type:
  45. //
  46. //      moveIKtoFK;
  47. //
  48. //</doc>
  49. /////////////////////////////////////////////////////////////////////////
  50. proc string getEndJointForHandle(string $handle)
  51. //
  52. // Given a handle, return the end joint that it controls.
  53. //
  54. {
  55.     string $endJoint;
  56.     string $endEffector = `ikHandle -q -endEffector $handle`;
  57.     if (size($endEffector)) {
  58.         string $connJoint[] = `listConnections -s 1 -d 0 ($endEffector+".translateX")`;
  59.         if (size($connJoint)) {
  60.             $endJoint = $connJoint[0];
  61.         }
  62.     }
  63.     return $endJoint;
  64. }
  65.  
  66. proc string getHandleForStartJoint(string $joint)
  67. {
  68.     string $result;
  69.     if (nodeType($joint) == "joint") {
  70.         string $connHandles[] = `listConnections -type ikHandle ($joint+".msg")`;
  71.         if (size($connHandles)) {
  72.             $result = $connHandles[0];
  73.         }
  74.     }
  75.     return $result;
  76. }
  77.  
  78. proc string ikBlendAttrName(string $obj)
  79. //
  80. // Given a handle or connected object, return the ikBlend or solverEnable
  81. // attribute as appropriate. (solverEnable is pre-5.0, ikBlend is post.)
  82. // Return "" if no attribute is found.    
  83. //
  84. {
  85.     string $attrName = ".ikBlend";
  86.     if (size(`ls ($obj+".solverEnable")`)) {
  87.         // In 5.0, the solverEnable attribute name was switched to
  88.         // ikBlend. 
  89.         //
  90.         $attrName = ".solverEnable";
  91.     }
  92.     if (size(`ls ($obj+$attrName)`)) {
  93.         return $attrName;
  94.     }
  95.     return "";
  96. }
  97.  
  98. proc int isFKhandle(string $handle)
  99. {
  100.     if (nodeType($handle) == "ikHandle") {
  101.         float $blendVal = `getAttr ($handle+".ikBlend")`;
  102.         if ($blendVal < 0.0001) {
  103.             return 1;
  104.         }
  105.     }
  106.     return 0;
  107. }
  108.  
  109. proc string[] findFKHandlesInHierarchy(string $handle)
  110. //
  111. // Find FK handles that are below the supplied handle or the
  112. // joints that it controls in the hierarchy.
  113. //
  114. {
  115.     string $result[];
  116.     string $startJoint = `ikHandle -q -sj $handle`;
  117.     
  118.     string $jointsBelow[] = `listRelatives -path -allDescendents -typ joint $startJoint`;
  119.     string $handlesBelow[] = `listRelatives -path -allDescendents -typ ikHandle $startJoint`;
  120.  
  121.     int $ii;
  122.     int $count = size($jointsBelow);
  123.     string $currItem = "";
  124.     for ($ii = ($count-1); $ii >= 0; $ii--) {
  125.         string $attrName = ikBlendAttrName($jointsBelow[$ii]);
  126.         if (size($attrName)) {
  127.             string $connHandles[] =
  128.                 `listConnections -type ikHandle ($handle+$attrName)`;
  129.             for ($conn in $connHandles) {
  130.                 if ($currItem != $conn && isFKhandle($conn)) {
  131.                     $result[size($result)] = $conn;
  132.                     $currItem = $conn;
  133.                 }
  134.             }
  135.         } else {
  136.             string $handle = getHandleForStartJoint($jointsBelow[$ii]);
  137.             if ("" != $handle) {
  138.                 $result[size($result)] = $handle;
  139.                 $currItem = $handle;
  140.             }
  141.         }
  142.     }
  143.  
  144.     $count = size($handlesBelow);
  145.     for ($ii = ($count-1); $ii >= 0; $ii--) {
  146.         string $item = $handlesBelow[$ii];
  147.         if ($currItem != $item && isFKhandle($item)) {
  148.             $result[size($result)] = $item;
  149.             $currItem = $item;
  150.         }
  151.     }
  152.  
  153.     string $resultNoDup[] = stringArrayRemoveDuplicates($result);
  154.     return $resultNoDup;
  155. }
  156.  
  157.  
  158. proc string findDriverParent(string $handle) {
  159.     string $result;
  160.     string $attrName = ikBlendAttrName($handle);
  161.     if (size($attrName)) {
  162.         string $drivers[] = `listConnections -s 0 -d 1 -exactType 1 -type transform ($handle+$attrName)`;
  163.         string $parent[] = `listRelatives -pa -p $handle`;
  164.         if (size($drivers) && size($parent)) {
  165.             for ($driver in $drivers) {
  166.                 if ($driver == $parent[0]) {
  167.                     $result = $driver;
  168.                     break;
  169.                 }
  170.             }
  171.         }
  172.     }
  173.     return $result;
  174. }
  175.  
  176. global proc moveIKtoFK()
  177. //
  178. //    Description:
  179. //        Move the ikHandle and its connected handle to the position of the FK
  180. //         joint.
  181. //
  182. {
  183.     string $selected[] = `ls -sl`;
  184.     int $nSelected = size($selected);
  185.     if (0 == $nSelected) {
  186.         error("Must select an ik handle, joint or object that connected to IK/FK.");
  187.     }
  188.     
  189.     // based on the selection, build a list of the handles we need to
  190.     // operate on
  191.     //
  192.     string $relatedHandles[];
  193.  
  194.     // Find handles for valid objects. Valid selected objects are:
  195.     //   joints
  196.     //   handles
  197.     //   objects connected to handles using "connect to ikfk"
  198.     //
  199.     for ($obj in $selected) {
  200.         if ("ikHandle" == nodeType($obj)) {
  201.             $relatedHandles[size($relatedHandles)] = $obj;
  202.         } else if ("joint" == nodeType($obj) ||
  203.                    "transform" == nodeType($obj)) {
  204.             string $attrName = ikBlendAttrName($obj);
  205.             if (size($attrName)) {
  206.                 string $connHandles[] =
  207.                     `listConnections -type ikHandle ($obj+$attrName)`;
  208.                 for ($conn in $connHandles) {
  209.                     $relatedHandles[size($relatedHandles)] = $conn;
  210.                 }
  211.             }
  212.         }
  213.     }
  214.  
  215.     // add handles in the hierarchy
  216.     //
  217.     string $hierarchyHandles[];
  218.     for ($handle in $relatedHandles) {
  219.         $hierarchyHandles[size($hierarchyHandles)] = $handle;
  220.         string $handlesBelow[] = findFKHandlesInHierarchy($handle);
  221.         for ($hierHandle in $handlesBelow) {
  222.             $hierarchyHandles[size($hierarchyHandles)] = $hierHandle;
  223.         }
  224.     }
  225.     string $handlesToMove[] = stringArrayRemoveDuplicates($hierarchyHandles);
  226.  
  227.     // Now calculate how to move the handles & move them
  228.     //
  229.     for ($handle in $handlesToMove) {
  230.         string $objectToMove = $handle;
  231.         
  232.         // get handle position
  233.         //
  234.         float $handlePos[] = `xform -q -t -ws $handle`;
  235.  
  236.         // get position of related end joint
  237.         //
  238.         string $endJoint = getEndJointForHandle($handle);
  239.         if (0 == size($endJoint)) {
  240.             continue;
  241.         }
  242.         float $endJointPos[] = `xform -q -t -ws $endJoint`;
  243.  
  244.         // is the handle parented to a "Connect To IK" driver object?
  245.         //
  246.         string $connectedDriver = findDriverParent($handle);
  247.         if (size($connectedDriver)) {
  248.             $objectToMove = $connectedDriver;
  249.         }
  250.  
  251.         string $moveCmd = ("move -r "+
  252.                            ($endJointPos[0]-$handlePos[0])+" "+
  253.                            ($endJointPos[1]-$handlePos[1])+" "+
  254.                            ($endJointPos[2]-$handlePos[2])+" "+
  255.                            $objectToMove);
  256.         evalEcho $moveCmd;
  257.     }
  258.  
  259.     if (0 == size($handlesToMove)) {
  260.         error("Must select an ik handle, joint or object that connected to IK/FK.");
  261.     }
  262. }
  263.